<a href='https://github.com/angular/angular.js/edit/master/docs/content/guide/$location.ngdoc?message=docs(guide%2FUsing $location)%3A%20describe%20your%20change...' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit">&nbsp;</i>Improve this Doc</a>


<h1 id="what-does-it-do-">What does it do?</h1>
<p>The <code>$location</code> service parses the URL in the browser address bar (based on <a href="https://developer.mozilla.org/en/window.location"><code>window.location</code></a>) and makes the URL available to
your application. Changes to the URL in the address bar are reflected into the <code>$location</code> service and
changes to <code>$location</code> are reflected into the browser address bar.</p>
<p><strong>The $location service:</strong></p>
<ul>
<li>Exposes the current URL in the browser address bar, so you can<ul>
<li>Watch and observe the URL.</li>
<li>Change the URL.</li>
</ul>
</li>
<li>Maintains synchronization between itself and the browser&#39;s URL when the user<ul>
<li>Changes the address in the browser&#39;s address bar.</li>
<li>Clicks the back or forward button in the browser (or clicks a History link).</li>
<li>Clicks on a link in the page.</li>
</ul>
</li>
<li>Represents the URL object as a set of methods (protocol, host, port, path, search, hash).</li>
</ul>
<h2 id="comparing-location-to-window-location-">Comparing <code>$location</code> to <code>window.location</code></h2>
<table class="table">
<thead>

  <tr>
    <th class="empty-corner-lt"></th>
    <th>window.location</th>
    <th>$location service</th>
  </tr>

</thead>
<tbody>

  <tr>
    <td class="head">purpose</td>
    <td>allow read/write access to the current browser location</td>
    <td>same</td>
  </tr>

  <tr>
    <td class="head">API</td>
    <td>exposes &quot;raw&quot; object with properties that can be directly modified</td>
    <td>exposes jQuery-style getters and setters</td>
  </tr>

  <tr>
    <td class="head">integration with AngularJS application life-cycle</td>
    <td>none</td>
    <td>knows about all internal life-cycle phases, integrates with <a href="api/ng/type/$rootScope.Scope#$watch">$watch</a>, ...</td>
  </tr>

  <tr>
    <td class="head">seamless integration with HTML5 API</td>
    <td>no</td>
    <td>yes (with a fallback for legacy browsers)</td>
  </tr>

  <tr>
    <td class="head">aware of docroot/context from which the application is loaded</td>
    <td>no - window.location.pathname returns &quot;/docroot/actual/path&quot;</td>
    <td>yes - $location.path() returns &quot;/actual/path&quot;</td>
  </tr>

</tbody>
</table>

<h2 id="when-should-i-use-location-">When should I use <code>$location</code>?</h2>
<p>Any time your application needs to react to a change in the current URL or if you want to change
the current URL in the browser.</p>
<h2 id="what-does-it-not-do-">What does it not do?</h2>
<p>It does not cause a full page reload when the browser URL is changed. To reload the page after
changing the URL, use the lower-level API, <code>$window.location.href</code>.</p>
<h1 id="general-overview-of-the-api">General overview of the API</h1>
<p>The <code>$location</code> service can behave differently, depending on the configuration that was provided to
it when it was instantiated. The default configuration is suitable for many applications, for
others customizing the configuration can enable new features.</p>
<p>Once the <code>$location</code> service is instantiated, you can interact with it via jQuery-style getter and
setter methods that allow you to get or change the current URL in the browser.</p>
<h2 id="-location-service-configuration"><code>$location</code> service configuration</h2>
<p>To configure the <code>$location</code> service, retrieve the
<a href="api/ng/provider/$locationProvider">$locationProvider</a> and set the parameters as follows:</p>
<ul>
<li><p><strong>html5Mode(mode)</strong>: <code>{boolean|Object}</code><br />
<code>false</code> or <code>{enabled: false}</code> (default) -
  see <a href="guide/$location#hashbang-mode-default-mode-">Hashbang mode</a><br />
<code>true</code> or <code>{enabled: true}</code> -
  see <a href="guide/$location#html5-mode">HTML5 mode</a><br />
<code>{..., requireBase: true/false}</code> (only affects HTML5 mode) -
  see <a href="guide/$location#relative-links">Relative links</a><br />
<code>{..., rewriteLinks: true/false/&#39;string&#39;}</code> (only affects HTML5 mode) -
  see <a href="guide/$location#html-link-rewriting">HTML link rewriting</a><br />
Default:</p>
<pre><code class="lang-j">{
  enabled: false,
  requireBase: true,
  rewriteLinks: true
}
</code></pre>
</li>
<li><p><strong>hashPrefix(prefix)</strong>: <code>{string}</code><br />
Prefix used for Hashbang URLs (used in Hashbang mode or in legacy browsers in HTML5 mode).<br />
Default: <code>&#39;!&#39;</code></p>
</li>
</ul>
<h3 id="example-configuration">Example configuration</h3>
<pre><code class="lang-js">$locationProvider.html5Mode(true).hashPrefix(&#39;*&#39;);
</code></pre>
<h2 id="getter-and-setter-methods">Getter and setter methods</h2>
<p><code>$location</code> service provides getter methods for read-only parts of the URL (absUrl, protocol, host,
port) and getter / setter methods for url, path, search, hash:</p>
<pre><code class="lang-js">// get the current path
$location.path();

// change the path
$location.path(&#39;/newValue&#39;)
</code></pre>
<p>All of the setter methods return the same <code>$location</code> object to allow chaining. For example, to
change multiple segments in one go, chain setters like this:</p>
<pre><code class="lang-js">$location.path(&#39;/newValue&#39;).search({key: value});
</code></pre>
<h2 id="replace-method">Replace method</h2>
<p>There is a special <code>replace</code> method which can be used to tell the $location service that the next
time the $location service is synced with the browser, the last history record should be replaced
instead of creating a new one. This is useful when you want to implement redirection, which would
otherwise break the back button (navigating back would retrigger the redirection). To change the
current URL without creating a new browser history record you can call:</p>
<pre><code class="lang-js">$location.path(&#39;/someNewPath&#39;);
$location.replace();
// or you can chain these as: $location.path(&#39;/someNewPath&#39;).replace();
</code></pre>
<p>Note that the setters don&#39;t update <code>window.location</code> immediately. Instead, the <code>$location</code> service is
aware of the <a href="api/ng/type/$rootScope.Scope">scope</a> life-cycle and coalesces multiple <code>$location</code>
mutations into one &quot;commit&quot; to the <code>window.location</code> object during the scope <code>$digest</code> phase. Since
multiple changes to the $location&#39;s state will be pushed to the browser as a single change, it&#39;s
enough to call the <code>replace()</code> method just once to make the entire &quot;commit&quot; a replace operation
rather than an addition to the browser history. Once the browser is updated, the $location service
resets the flag set by <code>replace()</code> method and future mutations will create new history records,
unless <code>replace()</code> is called again.</p>
<h3 id="setters-and-character-encoding">Setters and character encoding</h3>
<p>You can pass special characters to <code>$location</code> service and it will encode them according to rules
specified in <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>. When you access the methods:</p>
<ul>
<li>All values that are passed to <code>$location</code> setter methods, <code>path()</code>, <code>search()</code>, <code>hash()</code>, are
encoded.</li>
<li>Getters (calls to methods without parameters) return decoded values for the following methods
<code>path()</code>, <code>search()</code>, <code>hash()</code>.</li>
<li>When you call the <code>absUrl()</code> method, the returned value is a full url with its segments encoded.</li>
<li>When you call the <code>url()</code> method, the returned value is path, search and hash, in the form
<code>/path?search=a&amp;b=c#hash</code>. The segments are encoded as well.</li>
</ul>
<h1 id="hashbang-and-html5-modes">Hashbang and HTML5 Modes</h1>
<p><code>$location</code> service has two configuration modes which control the format of the URL in the browser
address bar: <strong>Hashbang mode</strong> (the default) and the <strong>HTML5 mode</strong> which is based on using the
<a href="https://html.spec.whatwg.org/multipage/browsers.html#the-history-interface">HTML5 History API</a>. Applications use the same API in
both modes and the <code>$location</code> service will work with appropriate URL segments and browser APIs to
facilitate the browser URL change and history management.</p>
<p><img src="img/guide/hashbang_vs_regular_url.jpg"></p>
<table class="table">
<thead>

  <tr>
    <th class="empty-corner-lt"></th>
    <th>Hashbang mode</th>
    <th>HTML5 mode</th>
  </tr>

</thead>
<tbody>

  <tr>
    <td class="head">configuration</td>
    <td>the default</td>
    <td>{ html5Mode: true }</td>
  </tr>

  <tr>
    <td class="head">URL format</td>
    <td>hashbang URLs in all browsers</td>
    <td>regular URLs in modern browser, hashbang URLs in old browser</td>
  </tr>

  <tr>
    <td class="head">&lt;a href=&quot;&quot;&gt; link rewriting</td>
    <td>no</td>
    <td>yes</td>
  </tr>

  <tr>
    <td class="head">requires server-side configuration</td>
    <td>no</td>
    <td>yes</td>
  </tr>
</tbody>
</table>

<h2 id="hashbang-mode-default-mode-">Hashbang mode (default mode)</h2>
<p>In this mode, <code>$location</code> uses Hashbang URLs in all browsers.
AngularJS also does not intercept and rewrite links in this mode. I.e. links work
as expected and also perform full page reloads when other parts of the url
than the hash fragment was changed.</p>
<h3 id="example">Example</h3>
<pre><code class="lang-js">it(&#39;should show example&#39;, function() {
  module(function($locationProvider) {
    $locationProvider.html5Mode(false);
    $locationProvider.hashPrefix(&#39;!&#39;);
  });
  inject(function($location) {
    // open http://example.com/base/index.html#!/a
    expect($location.absUrl()).toBe(&#39;http://example.com/base/index.html#!/a&#39;);
    expect($location.path()).toBe(&#39;/a&#39;);

    $location.path(&#39;/foo&#39;);
    expect($location.absUrl()).toBe(&#39;http://example.com/base/index.html#!/foo&#39;);

    expect($location.search()).toEqual({});
    $location.search({a: &#39;b&#39;, c: true});
    expect($location.absUrl()).toBe(&#39;http://example.com/base/index.html#!/foo?a=b&amp;c&#39;);

    $location.path(&#39;/new&#39;).search(&#39;x=y&#39;);
    expect($location.absUrl()).toBe(&#39;http://example.com/base/index.html#!/new?x=y&#39;);
  });
});
</code></pre>
<h2 id="html5-mode">HTML5 mode</h2>
<p>In HTML5 mode, the <code>$location</code> service getters and setters interact with the browser URL address
through the HTML5 history API. This allows for use of regular URL path and search segments,
instead of their hashbang equivalents. If the HTML5 History API is not supported by a browser, the
<code>$location</code> service will fall back to using the hashbang URLs automatically. This frees you from
having to worry about whether the browser displaying your app supports the history API  or not; the
<code>$location</code> service transparently uses the best available option.</p>
<ul>
<li>Opening a regular URL in a legacy browser -&gt; redirects to a hashbang URL</li>
<li>Opening hashbang URL in a modern browser -&gt; rewrites to a regular URL</li>
</ul>
<p>Note that in this mode, AngularJS intercepts all links (subject to the &quot;Html link rewriting&quot; rules below)
and updates the url in a way that never performs a full page reload.</p>
<h3 id="example">Example</h3>
<pre><code class="lang-js">it(&#39;should show example&#39;, function() {
  module(function($locationProvider) {
    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix(&#39;!&#39;);
  });
  inject(function($location) {
    // in browser with HTML5 history support:
    // open http://example.com/#!/a -&gt; rewrite to http://example.com/a
    // (replacing the http://example.com/#!/a history record)
    expect($location.path()).toBe(&#39;/a&#39;);

    $location.path(&#39;/foo&#39;);
    expect($location.absUrl()).toBe(&#39;http://example.com/foo&#39;);

    expect($location.search()).toEqual({});
    $location.search({a: &#39;b&#39;, c: true});
    expect($location.absUrl()).toBe(&#39;http://example.com/foo?a=b&amp;c&#39;);

    $location.path(&#39;/new&#39;).search(&#39;x=y&#39;);
    expect($location.url()).toBe(&#39;/new?x=y&#39;);
    expect($location.absUrl()).toBe(&#39;http://example.com/new?x=y&#39;);
  });
});

it(&#39;should show example (when browser doesn\&#39;t support HTML5 mode&#39;, function() {
  module(function($provide, $locationProvider) {
    $locationProvider.html5Mode(true);
    $locationProvider.hashPrefix(&#39;!&#39;);
    $provide.value(&#39;$sniffer&#39;, {history: false});
  });
  inject(initBrowser({ url: &#39;http://example.com/new?x=y&#39;, basePath: &#39;/&#39; }),
    function($location) {
    // in browser without html5 history support:
    // open http://example.com/new?x=y -&gt; redirect to http://example.com/#!/new?x=y
    // (again replacing the http://example.com/new?x=y history item)
    expect($location.path()).toBe(&#39;/new&#39;);
    expect($location.search()).toEqual({x: &#39;y&#39;});

    $location.path(&#39;/foo/bar&#39;);
    expect($location.path()).toBe(&#39;/foo/bar&#39;);
    expect($location.url()).toBe(&#39;/foo/bar?x=y&#39;);
    expect($location.absUrl()).toBe(&#39;http://example.com/#!/foo/bar?x=y&#39;);
  });
});
</code></pre>
<h3 id="fallback-for-legacy-browsers">Fallback for legacy browsers</h3>
<p>For browsers that support the HTML5 history API, <code>$location</code> uses the HTML5 history API to write
path and search. If the history API is not supported by a browser, <code>$location</code> supplies a Hashbang
URL. This frees you from having to worry about whether the browser viewing your app supports the
history API  or not; the <code>$location</code> service makes this transparent to you.</p>
<h3 id="html-link-rewriting">HTML link rewriting</h3>
<p>When you use HTML5 history API mode, you will not need special hashbang links. All you have to do
is specify regular URL links, such as: <code>&lt;a href=&quot;/some?foo=bar&quot;&gt;link&lt;/a&gt;</code></p>
<p>When a user clicks on this link,</p>
<ul>
<li>In a legacy browser, the URL changes to <code>/index.html#!/some?foo=bar</code></li>
<li>In a modern browser, the URL changes to <code>/some?foo=bar</code></li>
</ul>
<p>In cases like the following, links are not rewritten; instead, the browser will perform a full page
reload to the original link.</p>
<ul>
<li>Links that contain <code>target</code> element<br>
Example: <code>&lt;a href=&quot;/ext/link?a=b&quot; target=&quot;_self&quot;&gt;link&lt;/a&gt;</code></li>
<li>Absolute links that go to a different domain<br>
Example: <code>&lt;a href=&quot;http://angularjs.org/&quot;&gt;link&lt;/a&gt;</code></li>
<li>Links starting with &#39;/&#39; that lead to a different base path<br>
Example: <code>&lt;a href=&quot;/not-my-base/link&quot;&gt;link&lt;/a&gt;</code></li>
</ul>
<p>If <code>mode.rewriteLinks</code> is set to <code>false</code> in the <code>mode</code> configuration object passed to
<code>$locationProvider.html5Mode()</code>, the browser will perform a full page reload for every link.
<code>mode.rewriteLinks</code> can also be set to a string, which will enable link rewriting only on anchor
elements that have the given attribute.</p>
<p>For example, if <code>mode.rewriteLinks</code> is set to <code>&#39;internal-link&#39;</code>:</p>
<ul>
<li><code>&lt;a href=&quot;/some/path&quot; internal-link&gt;link&lt;/a&gt;</code> will be rewritten</li>
<li><code>&lt;a href=&quot;/some/path&quot;&gt;link&lt;/a&gt;</code> will perform a full page reload</li>
</ul>
<p>Note that <a href="guide/directive#normalization">attribute name normalization</a> does not apply here, so
<code>&#39;internalLink&#39;</code> will <strong>not</strong> match <code>&#39;internal-link&#39;</code>.</p>
<h3 id="relative-links">Relative links</h3>
<p>Be sure to check all relative links, images, scripts etc. AngularJS requires you to specify the url
base in the head of your main html file (<code>&lt;base href=&quot;/my-base/index.html&quot;&gt;</code>) unless <code>html5Mode.requireBase</code>
is set to <code>false</code> in the html5Mode definition object passed to <code>$locationProvider.html5Mode()</code>. With
that, relative urls will always be resolved to this base url, even if the initial url of the
document was different.</p>
<p>There is one exception: Links that only contain a hash fragment (e.g. <code>&lt;a href=&quot;#target&quot;&gt;</code>)
will only change <code>$location.hash()</code> and not modify the url otherwise. This is useful for scrolling
to anchors on the same page without needing to know on which page the user currently is.</p>
<h3 id="server-side">Server side</h3>
<p>Using this mode requires URL rewriting on server side, basically you have to rewrite all your links
to entry point of your application (e.g. index.html). Requiring a <code>&lt;base&gt;</code> tag is also important for
this case, as it allows AngularJS to differentiate between the part of the url that is the application
base and the path that should be handled by the application.</p>
<h3 id="base-href-constraints">Base href constraints</h3>
<p>The <code>$location</code> service is not able to function properly if the current URL is outside the URL given
as the base href. This can have subtle confusing consequences...</p>
<p>Consider a base href set as follows: <code>&lt;base href=&quot;/base/&quot;&gt;</code> (i.e. the application exists in the &quot;folder&quot;
called <code>/base</code>). The URL <code>/base</code> is actually outside the application (it refers to the <code>base</code> file found
in the root <code>/</code> folder).</p>
<p>If you wish to be able to navigate to the application via a URL such as <code>/base</code> then you should ensure that
you server is setup to redirect such requests to <code>/base/</code>.</p>
<p>See <a href="https://github.com/angular/angular.js/issues/14018">https://github.com/angular/angular.js/issues/14018</a> for more information.</p>
<h3 id="sending-links-among-different-browsers">Sending links among different browsers</h3>
<p>Because of rewriting capability in HTML5 mode, your users will be able to open regular url links in
legacy browsers and hashbang links in modern browser:</p>
<ul>
<li>Modern browser will rewrite hashbang URLs to regular URLs.</li>
<li>Older browsers will redirect regular URLs to hashbang URLs.</li>
</ul>
<h3 id="example">Example</h3>
<p>Here you can see two <code>$location</code> instances that show the difference between <strong>Html5 mode</strong> and <strong>Html5 Fallback mode</strong>.
Note that to simulate different levels of browser support, the <code>$location</code> instances are connected to
a fakeBrowser service, which you don&#39;t have to set up in actual projects.</p>
<p>Note that when you type hashbang url into the first browser (or vice versa) it doesn&#39;t rewrite /
redirect to regular / hashbang url, as this conversion happens only during parsing the initial URL
= on page reload.</p>
<p>In these examples we use <code>&lt;base href=&quot;/base/index.html&quot; /&gt;</code>. The inputs represent the address bar of the browser.</p>
<h4 id="browser-in-html5-mode">Browser in HTML5 mode</h4>
<p>

<div>
  <plnkr-opener example-path="examples/example-location-html5-mode"></plnkr-opener>

  <div class="runnable-example"
      path="examples/example-location-html5-mode"
      module="html5-mode"
      name="location-html5-mode">

  
    <div class="runnable-example-file" 
      name="index.html"
      language="html"
      type="html">
      <pre><code>&lt;div ng-controller=&quot;LocationController&quot;&gt;&#13;&#10;  &lt;div ng-address-bar&gt;&lt;/div&gt;&lt;br&gt;&lt;br&gt;&#13;&#10;  &lt;div&gt;&#13;&#10;    $location.protocol() = &lt;span ng-bind=&quot;$location.protocol()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.host() = &lt;span ng-bind=&quot;$location.host()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.port() = &lt;span ng-bind=&quot;$location.port()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.path() = &lt;span ng-bind=&quot;$location.path()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.search() = &lt;span ng-bind=&quot;$location.search()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.hash() = &lt;span ng-bind=&quot;$location.hash()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;  &lt;/div&gt;&#13;&#10;  &lt;div id=&quot;navigation&quot;&gt;&#13;&#10;    &lt;a href=&quot;http://www.example.com/base/first?a=b&quot;&gt;/base/first?a=b&lt;/a&gt; |&#13;&#10;    &lt;a href=&quot;http://www.example.com/base/sec/ond?flag#hash&quot;&gt;sec/ond?flag#hash&lt;/a&gt; |&#13;&#10;    &lt;a href=&quot;/other-base/another?search&quot;&gt;external&lt;/a&gt;&#13;&#10;  &lt;/div&gt;&#13;&#10;&lt;/div&gt;&#13;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="app.js"
      language="js"
      type="js">
      <pre><code>angular.module(&#39;html5-mode&#39;, [&#39;fake-browser&#39;, &#39;address-bar&#39;])&#13;&#10;&#10;// Configure the fakeBrowser. Do not set these values in actual projects.&#13;&#10;.constant(&#39;initUrl&#39;, &#39;http://www.example.com/base/path?a=b#h&#39;)&#13;&#10;.constant(&#39;baseHref&#39;, &#39;/base/index.html&#39;)&#13;&#10;.value(&#39;$sniffer&#39;, { history: true })&#13;&#10;&#10;.controller(&#39;LocationController&#39;, function($scope, $location) {&#13;&#10;  $scope.$location = {};&#13;&#10;  angular.forEach(&#39;protocol host port path search hash&#39;.split(&#39; &#39;), function(method) {&#13;&#10;   $scope.$location[method] = function() {&#13;&#10;     var result = $location[method]();&#13;&#10;     return angular.isObject(result) ? angular.toJson(result) : result;&#13;&#10;   };&#13;&#10;  });&#13;&#10;})&#13;&#10;&#10;.config(function($locationProvider) {&#13;&#10;  $locationProvider.html5Mode(true).hashPrefix(&#39;!&#39;);&#13;&#10;})&#13;&#10;&#10;.run(function($rootElement) {&#13;&#10;  $rootElement.on(&#39;click&#39;, function(e) { e.stopPropagation(); });&#13;&#10;});&#13;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="fakeBrowser.js"
      language="js"
      type="js">
      <pre><code>angular.module(&#39;fake-browser&#39;, [])&#13;&#10;&#10;.config(function($provide) {&#13;&#10; $provide.decorator(&#39;$browser&#39;, function($delegate, baseHref, initUrl) {&#13;&#10;&#10;  $delegate.onUrlChange = function(fn) {&#13;&#10;     this.urlChange = fn;&#13;&#10;   };&#13;&#10;&#10;  $delegate.url = function() {&#13;&#10;     return initUrl;&#13;&#10;  };&#13;&#10;&#10;  $delegate.defer = function(fn, delay) {&#13;&#10;     setTimeout(function() { fn(); }, delay || 0);&#13;&#10;   };&#13;&#10;&#10;  $delegate.baseHref = function() {&#13;&#10;     return baseHref;&#13;&#10;   };&#13;&#10;&#10;   return $delegate;&#13;&#10; });&#13;&#10;});&#13;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="addressBar.js"
      language="js"
      type="js">
      <pre><code>angular.module(&#39;address-bar&#39;, [])&#13;&#10;.directive(&#39;ngAddressBar&#39;, function($browser, $timeout) {&#13;&#10;   return {&#13;&#10;     template: &#39;Address: &lt;input id=&quot;addressBar&quot; type=&quot;text&quot; style=&quot;width: 400px&quot; &gt;&#39;,&#13;&#10;     link: function(scope, element, attrs) {&#13;&#10;       var input = element.children(&#39;input&#39;), delay;&#13;&#10;&#10;       input.on(&#39;keypress keyup keydown&#39;, function(event) {&#13;&#10;               delay = (!delay ? $timeout(fireUrlChange, 250) : null);&#13;&#10;               event.stopPropagation();&#13;&#10;             })&#13;&#10;            .val($browser.url());&#13;&#10;&#10;       $browser.url = function(url) {&#13;&#10;         return url ? input.val(url) : input.val();&#13;&#10;       };&#13;&#10;&#10;       function fireUrlChange() {&#13;&#10;         delay = null;&#13;&#10;         $browser.urlChange(input.val());&#13;&#10;       }&#13;&#10;     }&#13;&#10;   };&#13;&#10; });&#13;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="protractor.js"
      type="protractor"
      language="js">
      <pre><code>var addressBar = element(by.css(&quot;#addressBar&quot;)),&#13;&#10;    url = &#39;http://www.example.com/base/path?a=b#h&#39;;&#13;&#10;&#10;&#10;it(&quot;should show fake browser info on load&quot;, function() {&#13;&#10;  expect(addressBar.getAttribute(&#39;value&#39;)).toBe(url);&#13;&#10;&#10;  expect(element(by.binding(&#39;$location.protocol()&#39;)).getText()).toBe(&#39;http&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.host()&#39;)).getText()).toBe(&#39;www.example.com&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.port()&#39;)).getText()).toBe(&#39;80&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.path()&#39;)).getText()).toBe(&#39;/path&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.search()&#39;)).getText()).toBe(&#39;{&quot;a&quot;:&quot;b&quot;}&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.hash()&#39;)).getText()).toBe(&#39;h&#39;);&#13;&#10;&#10;});&#13;&#10;&#10;it(&quot;should change $location accordingly&quot;, function() {&#13;&#10;  var navigation = element.all(by.css(&quot;#navigation a&quot;));&#13;&#10;&#10;  navigation.get(0).click();&#13;&#10;&#10;  expect(addressBar.getAttribute(&#39;value&#39;)).toBe(&quot;http://www.example.com/base/first?a=b&quot;);&#13;&#10;&#10;  expect(element(by.binding(&#39;$location.protocol()&#39;)).getText()).toBe(&#39;http&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.host()&#39;)).getText()).toBe(&#39;www.example.com&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.port()&#39;)).getText()).toBe(&#39;80&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.path()&#39;)).getText()).toBe(&#39;/first&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.search()&#39;)).getText()).toBe(&#39;{&quot;a&quot;:&quot;b&quot;}&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.hash()&#39;)).getText()).toBe(&#39;&#39;);&#13;&#10;&#10;&#10;  navigation.get(1).click();&#13;&#10;&#10;  expect(addressBar.getAttribute(&#39;value&#39;)).toBe(&quot;http://www.example.com/base/sec/ond?flag#hash&quot;);&#13;&#10;&#10;  expect(element(by.binding(&#39;$location.protocol()&#39;)).getText()).toBe(&#39;http&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.host()&#39;)).getText()).toBe(&#39;www.example.com&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.port()&#39;)).getText()).toBe(&#39;80&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.path()&#39;)).getText()).toBe(&#39;/sec/ond&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.search()&#39;)).getText()).toBe(&#39;{&quot;flag&quot;:true}&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.hash()&#39;)).getText()).toBe(&#39;hash&#39;);&#13;&#10;});&#13;</code></pre>
    </div>
  

    <iframe class="runnable-example-frame" src="examples/example-location-html5-mode/index.html" name="example-location-html5-mode"></iframe>
  </div>
</div>


</p>
<h4 id="browser-in-html5-fallback-mode-hashbang-mode-">Browser in HTML5 Fallback mode (Hashbang mode)</h4>
<p>

<div>
  <plnkr-opener example-path="examples/example-location-hashbang-mode"></plnkr-opener>

  <div class="runnable-example"
      path="examples/example-location-hashbang-mode"
      module="hashbang-mode"
      name="location-hashbang-mode">

  
    <div class="runnable-example-file" 
      name="index.html"
      language="html"
      type="html">
      <pre><code>&lt;div ng-controller=&quot;LocationController&quot;&gt;&#13;&#10;  &lt;div ng-address-bar&gt;&lt;/div&gt;&lt;br&gt;&lt;br&gt;&#13;&#10;  &lt;div&gt;&#13;&#10;    $location.protocol() = &lt;span ng-bind=&quot;$location.protocol()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.host() = &lt;span ng-bind=&quot;$location.host()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.port() = &lt;span ng-bind=&quot;$location.port()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.path() = &lt;span ng-bind=&quot;$location.path()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.search() = &lt;span ng-bind=&quot;$location.search()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;    $location.hash() = &lt;span ng-bind=&quot;$location.hash()&quot;&gt;&lt;/span&gt; &lt;br&gt;&#13;&#10;  &lt;/div&gt;&#13;&#10;  &lt;div id=&quot;navigation&quot;&gt;&#13;&#10;    &lt;a href=&quot;http://www.example.com/base/first?a=b&quot;&gt;/base/first?a=b&lt;/a&gt; |&#13;&#10;    &lt;a href=&quot;http://www.example.com/base/sec/ond?flag#hash&quot;&gt;sec/ond?flag#hash&lt;/a&gt; |&#13;&#10;    &lt;a href=&quot;/other-base/another?search&quot;&gt;external&lt;/a&gt;&#13;&#10;  &lt;/div&gt;&#13;&#10;&lt;/div&gt;&#13;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="app.js"
      language="js"
      type="js">
      <pre><code>angular.module(&#39;hashbang-mode&#39;, [&#39;fake-browser&#39;, &#39;address-bar&#39;])&#13;&#10;&#10;// Configure the fakeBrowser. Do not set these values in actual projects.&#13;&#10;.constant(&#39;initUrl&#39;, &#39;http://www.example.com/base/index.html#!/path?a=b#h&#39;)&#13;&#10;.constant(&#39;baseHref&#39;, &#39;/base/index.html&#39;)&#13;&#10;.value(&#39;$sniffer&#39;, { history: false })&#13;&#10;&#10;.config(function($locationProvider) {&#13;&#10;  $locationProvider.html5Mode(true).hashPrefix(&#39;!&#39;);&#13;&#10;})&#13;&#10;&#10;.controller(&#39;LocationController&#39;, function($scope, $location) {&#13;&#10;  $scope.$location = {};&#13;&#10;  angular.forEach(&#39;protocol host port path search hash&#39;.split(&#39; &#39;), function(method) {&#13;&#10;    $scope.$location[method] = function() {&#13;&#10;      var result = $location[method]();&#13;&#10;      return angular.isObject(result) ? angular.toJson(result) : result;&#13;&#10;    };&#13;&#10;  });&#13;&#10;})&#13;&#10;&#10;.run(function($rootElement) {&#13;&#10;  $rootElement.on(&#39;click&#39;, function(e) {&#13;&#10;    e.stopPropagation();&#13;&#10;  });&#13;&#10;});&#13;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="fakeBrowser.js"
      language="js"
      type="js">
      <pre><code>angular.module(&#39;fake-browser&#39;, [])&#13;&#10;&#10;.config(function($provide) {&#13;&#10; $provide.decorator(&#39;$browser&#39;, function($delegate, baseHref, initUrl) {&#13;&#10;&#10;  $delegate.onUrlChange = function(fn) {&#13;&#10;     this.urlChange = fn;&#13;&#10;   };&#13;&#10;&#10;  $delegate.url = function() {&#13;&#10;     return initUrl;&#13;&#10;  };&#13;&#10;&#10;  $delegate.defer = function(fn, delay) {&#13;&#10;     setTimeout(function() { fn(); }, delay || 0);&#13;&#10;   };&#13;&#10;&#10;  $delegate.baseHref = function() {&#13;&#10;     return baseHref;&#13;&#10;   };&#13;&#10;&#10;   return $delegate;&#13;&#10; });&#13;&#10;});&#13;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="addressBar.js"
      language="js"
      type="js">
      <pre><code>angular.module(&#39;address-bar&#39;, [])&#13;&#10;.directive(&#39;ngAddressBar&#39;, function($browser, $timeout) {&#13;&#10;   return {&#13;&#10;     template: &#39;Address: &lt;input id=&quot;addressBar&quot; type=&quot;text&quot; style=&quot;width: 400px&quot; &gt;&#39;,&#13;&#10;     link: function(scope, element, attrs) {&#13;&#10;       var input = element.children(&#39;input&#39;), delay;&#13;&#10;&#10;       input.on(&#39;keypress keyup keydown&#39;, function(event) {&#13;&#10;               delay = (!delay ? $timeout(fireUrlChange, 250) : null);&#13;&#10;               event.stopPropagation();&#13;&#10;             })&#13;&#10;            .val($browser.url());&#13;&#10;&#10;       $browser.url = function(url) {&#13;&#10;         return url ? input.val(url) : input.val();&#13;&#10;       };&#13;&#10;&#10;       function fireUrlChange() {&#13;&#10;         delay = null;&#13;&#10;         $browser.urlChange(input.val());&#13;&#10;       }&#13;&#10;     }&#13;&#10;   };&#13;&#10; });&#13;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="protractor.js"
      type="protractor"
      language="js">
      <pre><code>var addressBar = element(by.css(&quot;#addressBar&quot;)),&#13;&#10;     url = &#39;http://www.example.com/base/index.html#!/path?a=b#h&#39;;&#13;&#10;&#10;it(&quot;should show fake browser info on load&quot;, function() {&#13;&#10;  expect(addressBar.getAttribute(&#39;value&#39;)).toBe(url);&#13;&#10;&#10;  expect(element(by.binding(&#39;$location.protocol()&#39;)).getText()).toBe(&#39;http&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.host()&#39;)).getText()).toBe(&#39;www.example.com&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.port()&#39;)).getText()).toBe(&#39;80&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.path()&#39;)).getText()).toBe(&#39;/path&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.search()&#39;)).getText()).toBe(&#39;{&quot;a&quot;:&quot;b&quot;}&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.hash()&#39;)).getText()).toBe(&#39;h&#39;);&#13;&#10;&#10;});&#13;&#10;&#10;it(&quot;should change $location accordingly&quot;, function() {&#13;&#10;  var navigation = element.all(by.css(&quot;#navigation a&quot;));&#13;&#10;&#10;  navigation.get(0).click();&#13;&#10;&#10;  expect(addressBar.getAttribute(&#39;value&#39;)).toBe(&quot;http://www.example.com/base/index.html#!/first?a=b&quot;);&#13;&#10;&#10;  expect(element(by.binding(&#39;$location.protocol()&#39;)).getText()).toBe(&#39;http&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.host()&#39;)).getText()).toBe(&#39;www.example.com&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.port()&#39;)).getText()).toBe(&#39;80&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.path()&#39;)).getText()).toBe(&#39;/first&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.search()&#39;)).getText()).toBe(&#39;{&quot;a&quot;:&quot;b&quot;}&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.hash()&#39;)).getText()).toBe(&#39;&#39;);&#13;&#10;&#10;&#10;  navigation.get(1).click();&#13;&#10;&#10;  expect(addressBar.getAttribute(&#39;value&#39;)).toBe(&quot;http://www.example.com/base/index.html#!/sec/ond?flag#hash&quot;);&#13;&#10;&#10;  expect(element(by.binding(&#39;$location.protocol()&#39;)).getText()).toBe(&#39;http&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.host()&#39;)).getText()).toBe(&#39;www.example.com&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.port()&#39;)).getText()).toBe(&#39;80&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.path()&#39;)).getText()).toBe(&#39;/sec/ond&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.search()&#39;)).getText()).toBe(&#39;{&quot;flag&quot;:true}&#39;);&#13;&#10;  expect(element(by.binding(&#39;$location.hash()&#39;)).getText()).toBe(&#39;hash&#39;);&#13;&#10;&#10;});&#13;</code></pre>
    </div>
  

    <iframe class="runnable-example-frame" src="examples/example-location-hashbang-mode/index.html" name="example-location-hashbang-mode"></iframe>
  </div>
</div>


</p>
<h1 id="caveats">Caveats</h1>
<h2 id="page-reload-navigation">Page reload navigation</h2>
<p>The <code>$location</code> service allows you to change only the URL; it does not allow you to reload the
page. When you need to change the URL and reload the page or navigate to a different page, please
use a lower level API, <a href="api/ng/service/$window">$window.location.href</a>.</p>
<h2 id="using-location-outside-of-the-scope-life-cycle">Using $location outside of the scope life-cycle</h2>
<p><code>$location</code> knows about AngularJS&#39;s <a href="api/ng/type/$rootScope.Scope">scope</a> life-cycle. When a URL changes in
the browser it updates the <code>$location</code> and calls <code>$apply</code> so that all
<a href="api/ng/type/$rootScope.Scope#$watch">$watchers</a> /
<a href="api/ng/type/$compile.directive.Attributes#$observe">$observers</a> are notified.
When you change the <code>$location</code> inside the <code>$digest</code> phase everything is ok; <code>$location</code> will
propagate this change into browser and will notify all the <a href="api/ng/type/$rootScope.Scope#$watch">$watchers</a> /
<a href="api/ng/type/$compile.directive.Attributes#$observe">$observers</a>.
When you want to change the <code>$location</code> from outside AngularJS (for example, through a DOM Event or
during testing) - you must call <code>$apply</code> to propagate the changes.</p>
<h2 id="-location-path-and-or-prefixes">$location.path() and ! or / prefixes</h2>
<p>A path should always begin with forward slash (<code>/</code>); the <code>$location.path()</code> setter will add the
forward slash if it is missing.</p>
<p>Note that the <code>!</code> prefix in the hashbang mode is not part of <code>$location.path()</code>; it is actually
<code>hashPrefix</code>.</p>
<h2 id="crawling-your-app">Crawling your app</h2>
<p>To allow indexing of your AJAX application, you have to add special meta tag in the head section of
your document:</p>
<pre><code class="lang-html">&lt;meta name=&quot;fragment&quot; content=&quot;!&quot; /&gt;
</code></pre>
<p>This will cause crawler bot to request links with <code>_escaped_fragment_</code> param so that your server
can recognize the crawler and serve a HTML snapshots. For more information about this technique,
see <a href="http://code.google.com/web/ajaxcrawling/docs/specification.html">Making AJAX Applications
Crawlable</a>.</p>
<h1 id="testing-with-the-location-service">Testing with the $location service</h1>
<p>When using <code>$location</code> service during testing, you are outside of the angular&#39;s <a href="api/ng/type/$rootScope.Scope">scope</a> life-cycle. This means it&#39;s your responsibility to call <code>scope.$apply()</code>.</p>
<pre><code class="lang-js">describe(&#39;serviceUnderTest&#39;, function() {
  beforeEach(module(function($provide) {
    $provide.factory(&#39;serviceUnderTest&#39;, function($location) {
      // whatever it does...
    });
  });

  it(&#39;should...&#39;, inject(function($location, $rootScope, serviceUnderTest) {
    $location.path(&#39;/new/path&#39;);
    $rootScope.$apply();

    // test whatever the service should do...

  }));
});
</code></pre>
<h1 id="migrating-from-earlier-angularjs-releases">Migrating from earlier AngularJS releases</h1>
<p>In earlier releases of AngularJS, <code>$location</code> used <code>hashPath</code> or <code>hashSearch</code> to process path and
search methods. With this release, the <code>$location</code> service processes path and search methods and
then uses the information it obtains to compose hashbang URLs (such as
<code>http://server.com/#!/path?search=a</code>), when necessary.</p>
<h2 id="changes-to-your-code">Changes to your code</h2>
<table class="table">
  <thead>
    <tr class="head">
      <th>Navigation inside the app</th>
      <th>Change to</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>$location.href = value<br />$location.hash = value<br />$location.update(value)<br
/>$location.updateHash(value)</td>
      <td>$location.path(path).search(search)</td>
    </tr>

    <tr>
      <td>$location.hashPath = path</td>
      <td>$location.path(path)</td>
    </tr>

    <tr>
      <td>$location.hashSearch = search</td>
      <td>$location.search(search)</td>
    </tr>

    <tr class="head">
      <th>Navigation outside the app</td>
      <th>Use lower level API</td>
    </tr>

    <tr>
      <td>$location.href = value<br />$location.update(value)</td>
      <td>$window.location.href = value</td>
    </tr>

    <tr>
      <td>$location[protocol | host | port | path | search]</td>
      <td>$window.location[protocol | host | port | path | search]</td>
    </tr>

    <tr class="head">
      <th>Read access</td>
      <th>Change to</td>
    </tr>

    <tr>
      <td>$location.hashPath</td>
      <td>$location.path()</td>
    </tr>

    <tr>
      <td>$location.hashSearch</td>
      <td>$location.search()</td>
    </tr>

    <tr>
      <td>$location.href<br />$location.protocol<br />$location.host<br />$location.port<br
/>$location.hash</td>
      <td>$location.absUrl()<br />$location.protocol()<br />$location.host()<br />$location.port()<br
/>$location.path() + $location.search()</td>
    </tr>

    <tr>
      <td>$location.path<br />$location.search</td>
      <td>$window.location.path<br />$window.location.search</td>
    </tr>
  </tbody>
</table>

<h2 id="two-way-binding-to-location">Two-way binding to $location</h2>
<p>Because <code>$location</code> uses getters/setters, you can use <code>ng-model-options=&quot;{ getterSetter: true }&quot;</code>
to bind it to <code>ngModel</code>:</p>
<p>

<div>
  <plnkr-opener example-path="examples/example-location-two-way-binding"></plnkr-opener>

  <div class="runnable-example"
      path="examples/example-location-two-way-binding"
      module="locationExample"
      name="location-two-way-binding">

  
    <div class="runnable-example-file" 
      name="index.html"
      language="html"
      type="html">
      <pre><code>&lt;div ng-controller=&quot;LocationController&quot;&gt;&#13;&#10;  &lt;input type=&quot;text&quot; ng-model=&quot;locationPath&quot; ng-model-options=&quot;{ getterSetter: true }&quot; /&gt;&#13;&#10;&lt;/div&gt;&#13;</code></pre>
    </div>
  
    <div class="runnable-example-file" 
      name="script.js"
      language="js"
      type="js">
      <pre><code>angular.module(&#39;locationExample&#39;, [])&#13;&#10;.controller(&#39;LocationController&#39;, [&#39;$scope&#39;, &#39;$location&#39;, function($scope, $location) {&#13;&#10;  $scope.locationPath = function(newLocation) {&#13;&#10;    return $location.path(newLocation);&#13;&#10;  };&#13;&#10;}]);&#13;</code></pre>
    </div>
  

    <iframe class="runnable-example-frame" src="examples/example-location-two-way-binding/index.html" name="example-location-two-way-binding"></iframe>
  </div>
</div>


</p>
<h1 id="related-api">Related API</h1>
<ul>
<li><a href="api/ng/service/$location"><code>$location</code> API</a></li>
</ul>


